cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

file(GLOB_RECURSE SRC_FILES src/*.*)
add_library(DiarySqlDriverPq MODULE ${SRC_FILES})

if (MSVC)
    set(libpqxx_DIR ${CMAKE_CURRENT_SOURCE_DIR}/pqxx/lib/cmake/libpqxx)
    find_package(libpqxx REQUIRED)
    target_link_libraries(DiarySqlDriverPq PRIVATE libpqxx::pqxx DiaryCore fmt::fmt)
elseif(UNIX)
    find_package(PkgConfig REQUIRED)
    pkg_search_module(pqxx REQUIRED IMPORTED_TARGET libpqxx)
    target_link_libraries(DiarySqlDriverPq PRIVATE PkgConfig::pqxx DiaryCore fmt::fmt)
endif()

set_target_properties(
    DiarySqlDriverPq
    PROPERTIES
    DEBUG_POSTFIX ""
    SUFFIX ".sqldriver"
    FOLDER "Ext/SQL Drivers"
	  LIBRARY_OUTPUT_DIRECTORY "$<TARGET_FILE_DIR:DiaryApp>/sql"
)

if (TARGET DiaryApp AND WIN32)
    add_custom_command(
        TARGET DiarySqlDriverPq
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:DiaryApp>/sql/$<TARGET_FILE_BASE_NAME:DiarySqlDriverPq>
        COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/pqxx/bin/" $<TARGET_FILE_DIR:DiaryApp>/sql/$<TARGET_FILE_BASE_NAME:DiarySqlDriverPq>
    )
endif()
